package com.neusoft.elmboot.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.elmboot.po.DeliveryAddress;
import com.neusoft.elmboot.po.DeliveryAddressDTO;
import com.neusoft.elmboot.service.DeliveryAddressService;

@RestController
@RequestMapping("/DeliveryAddressController")
public class DeliveryAddressController {
	@Autowired
	private DeliveryAddressService deliveryAddressService;
	
	@RequestMapping("/listDeliveryAddressByUserId")
    public List<DeliveryAddressDTO> listDeliveryAddressByUserId(DeliveryAddress deliveryAddress) throws Exception {
        List<DeliveryAddress> deliveryAddressList = deliveryAddressService.listDeliveryAddressByUserId(deliveryAddress.getUserId());
        
        // 将 DeliveryAddress 转换为 DeliveryAddressDTO
        List<DeliveryAddressDTO> dtoList = new ArrayList<>();
        for (DeliveryAddress da : deliveryAddressList) {
            DeliveryAddressDTO dto = new DeliveryAddressDTO();
            dto.setDaId(da.getDaId());
            dto.setContactName(da.getContactName());
            dto.setContactSex(da.getContactSex());
            dto.setContactTel(da.getContactTel());
            dto.setAddress(da.getAddress());
            dtoList.add(dto);
        }
        
        return dtoList;
    }

	@RequestMapping("/getDeliveryAddressById")
    public DeliveryAddressDTO getDeliveryAddressById(DeliveryAddress deliveryAddress) throws Exception {
        DeliveryAddress da = deliveryAddressService.getDeliveryAddressById(deliveryAddress.getDaId());

        // 转换为 DTO
        DeliveryAddressDTO dto = new DeliveryAddressDTO();
        dto.setDaId(da.getDaId());
        dto.setContactName(da.getContactName());
        dto.setContactSex(da.getContactSex());
        dto.setContactTel(da.getContactTel());
        dto.setAddress(da.getAddress());

        return dto;
    }

	@RequestMapping("/saveDeliveryAddress")
	public int saveDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception {
		return deliveryAddressService.saveDeliveryAddress(deliveryAddress);
	}

	@RequestMapping("/updateDeliveryAddress")
	public int updateDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception {
		return deliveryAddressService.updateDeliveryAddress(deliveryAddress);
	}

	@RequestMapping("/removeDeliveryAddress")
	public int removeDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception {
		return deliveryAddressService.removeDeliveryAddress(deliveryAddress.getDaId());
	}
}
